package com.tb.attendancesystem.student;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.tb.attendancesystem.R;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.Call;

/**
 * 学生端扫码返回结果
 */

public class ResultActivity extends AppCompatActivity implements View.OnClickListener {

    public static final String TAG = ResultActivity.class.getSimpleName();

    private TextView tvResult;
    private String resultStr;
    private String sno;
    private Button btBack;

    //服务器返回结果
    private static final int REQUEST_ERROR = -1;
    private static final int NOT_LEARN = 0;
    private static final int SUCCESS = 1;
    private static final int NOT_COURSE = 2;

    //加载资源
    private void loadRes() {
        resultStr = getIntent().getStringExtra("result_string");
        sno = getIntent().getStringExtra("sno");
        tvResult = (TextView) findViewById(R.id.tv_result);
        btBack = (Button) findViewById(R.id.button_back);
    }

    //注册监听
    private void register() {
        btBack.setOnClickListener(this);
    }


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_s_result);
        loadRes();
        register();
        showResult(resultStr);              //显示扫码结果
    }

    //显示扫码结果
    private void showResult(String codeStr) {
        OkHttpUtils.get().url(getResources().getString(R.string.host_url) + "/AttendanceSystem/ScanResult")
                .addParams("codeStr", codeStr)
                .addParams("sno", sno)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        responseDeal(response);
                    }
                });
    }

    //对服务器返回的结果进行处理
    private void responseDeal(String response) {
        Log.d(TAG, "服务器返回的结果是：" + response);
        String information = "";
        try {
            JSONObject jsonObject = new JSONObject(response);
            int status = jsonObject.getInt("status");

            if (status == REQUEST_ERROR) {
                //请求错误
                information = "服务器请求错误！";
            } else if (status == NOT_COURSE) {
                //不是签到的二维码信息
                information = "请扫描教师正在展示的二维码...";
            } else{
                information += jsonObject.getString("course_name") + "\n";
                information += jsonObject.getString("cid") + "\n";
                information += jsonObject.getString("date") + "\n";
                information += "第" + jsonObject.getString("times") + "次点到\n";
                if (status == NOT_LEARN) {
                    //没选该课
                    information += "签到失败，你没有选修该课程，无需签到。";
                } else {
                    //签到成功
                    information += "签到成功！";
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        tvResult.setText(information);
    }


    @Override
    public void onClick(View v) {
        if (v.equals(btBack)) {
            finish();
        }
    }
}
